﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Threading;
using System.Threading;
using System.Globalization;
using System.Linq;
using System.IO;

namespace Clipper
{
	/// <summary>
	/// App.xaml 的交互逻辑
	/// </summary>
	public partial class App : Application
	{
        public App()
        {
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Application_DispatcherUnhandledException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            
        }


        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                string errorMsg =UIHelper.GetResourceText("NoWindowExc");
                UIHelper.Show(errorMsg + ex.Message + Environment.NewLine + ex.StackTrace);
               
            }
            catch
            {
                UIHelper.ShowKeyMessage("ExitExc");
            }
        }

        void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {

            try
            {
                string errorMsg = UIHelper.GetResourceText("NoWindowExc");
                //msg msg = new msg(errorMsg + e.Exception.Message);
                //msg.ShowDialog();
                UIHelper.Show(errorMsg + e.Exception.Message);
                e.Handled = true;
            }
            catch
            {
                //Show("不可恢复的WPF窗体线程异常，应用程序将退出！");
                UIHelper.ShowKeyMessage("ExitExc");
            }
        }

        public static string Culture { get; set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //GetCulture();
        }
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            //SetCulture();
            
            //MessageBoxManager.Unregister();
        }

        #region 语言Method
        private void GetCulture()
        {
            Culture = string.Empty;
            try
            {
                Culture = Clipper.Properties.Settings.Default.Culture.Trim();
            }
            catch (Exception)
            {
            }
            Culture = string.IsNullOrEmpty(Culture) ? "en-US" : Culture;

            //update culture
            UpdateCulture();
        }
        private void SetCulture()
        {
            try
            {
                Clipper.Properties.Settings.Default.Culture = Culture;
                Clipper.Properties.Settings.Default.Save();
            }
            catch (Exception)
            {
            }
        }
        public static void UpdateCulture()
        {
            string requestedCulture = string.Format(@"Resources\StringResource.{0}.xaml", Culture);
            ResourceDictionary resourceDictionary = null;
            //if (File.Exists(requestedCulture))
            //{
            try
            {
                resourceDictionary = Application.LoadComponent(new Uri(requestedCulture, UriKind.Relative)) as ResourceDictionary;
            }
            catch
            {

            }
            if (resourceDictionary == null)
            {
                requestedCulture = @"Resources\StringResource.xaml";
                resourceDictionary = Application.LoadComponent(new Uri(requestedCulture, UriKind.Relative)) as ResourceDictionary;
             }
            if (resourceDictionary != null)
            {
                //Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Culture);

            //MessageBoxManager.Unregister();
            //MessageBoxManager.OK = Application.Current.TryFindResource("BtnOK").ToString();
            //MessageBoxManager.Register();
        }
        #endregion


	}
}